package org.x.usermod.controller;

import jakarta.validation.Valid;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.x.commonmod.enums.LogAsync;
import org.x.commonmod.config.Result;
import org.x.usermod.dto.*;
import org.x.usermod.entity.SysUserRealnameAuth;
import org.x.usermod.service.ISysUserService;
import org.x.usermod.vo.UserInfoVO;
import org.x.usermod.dto.UpdateProfileDTO;

@RestController
@RequestMapping("/api/user")
public class SysUserController {
    private final ISysUserService ISysUserService;

    public SysUserController(ISysUserService ISysUserService) {
        this.ISysUserService = ISysUserService;
    }

    //创建用户
    @LogAsync("创建用户（SysUser表）") // 使用默认参数记录
    @PostMapping("/create")
    public Result createUser(@RequestBody @Valid CreateUserDTO createUserDTO) {
        return ISysUserService.createUserWithValidation(createUserDTO);
    }

    @LogAsync("更新用户")
    @PutMapping("/update")
    @Transactional
    public Result updateUser(@RequestBody @Valid UpdateUserDTO updateUserDTO) {
        return Result.ok( ISysUserService.updateUser(updateUserDTO));
    }

    @LogAsync("逻辑删除用户")
    @DeleteMapping("/delete/{id}")
    @Transactional
    public Result deleteUser(@PathVariable @Valid Long id) {
        return ISysUserService.removeById(id);
    }

    @LogAsync("逻辑恢复用户")
    @PostMapping("/restore/{id}")
    @Transactional
    public Result restoreUser(@PathVariable @Valid Long id) {
        return ISysUserService.restoreById(id);
    }

    //根据ID查询用户
    @LogAsync("根据ID查询用户")
    @GetMapping("/get/{id}")
    public Result getUserById(@PathVariable @Valid Long id) {
        return ISysUserService.getById(id);
    }

    //分页查询用户列表
    @LogAsync("分页查询用户列表")
    @GetMapping("/list")
    public Result listUser( ListDto listDto){
        return Result.ok(ISysUserService.List(listDto));
    }

    //重置密码
    @LogAsync("修改密码")
    @PutMapping("/password")
    @Transactional
    public Result resetPassword(@RequestBody @Valid ResetPasswordDTO resetPasswordDTO) {
        return ISysUserService.resetPassword(resetPasswordDTO);
    }
    //修改用户的手机号
    @LogAsync("修改用户的手机号")
    @PutMapping("/mobile")
    @Transactional
    public Result resetMobile(@RequestBody @Valid ResetMobileDTO resetMobileDTO) {
        return ISysUserService.resetMobile(resetMobileDTO);
    }

    //获取用户权限信息
    @LogAsync("获取用户权限信息")
    @GetMapping("/authInfo")
    public UserInfoVO getUserAuthInfo() {
        return ISysUserService.getUserAuthInfo();
    }

    //获取当前登录用户的详细信息（包括基本信息和扩展信息）
    @LogAsync("获取当前登录用户的详细信息")
    @GetMapping("/profile")
    public PersonalInfoDTO getProfile() {
        return ISysUserService.getProfile();
    }
    //修改用户信息
    @LogAsync("修改用户信息")
    @PutMapping("/updateProfile")
    @Transactional
    public Result updateProfile(@RequestBody @Valid UpdateProfileDTO updateProfileDTO) {
        return ISysUserService.updateProfile(updateProfileDTO);
    }

    //获取SysUserRealnameAuth信息
    @LogAsync("获取SysUserRealnameAuth信息")
    @GetMapping("/auth-status")
    public SysUserRealnameAuth getAuthStatus(@RequestParam @Valid Long userId) {
        return ISysUserService.getSysUserRealnameAuth(userId);
    }

    //提交实名认证
    @LogAsync("提交实名认证")
    @PostMapping("/auth-submit")
    @Transactional
    public Result submitRealnameAuth(@RequestBody @Valid SURrealnamAuthDTO sysUserRealnameAuthDTO, @RequestParam @Valid Long userId) {
        return ISysUserService.submitRealnameAuth(sysUserRealnameAuthDTO,userId);
    }

    //分页获取待审核的实名认证信息列表
    @LogAsync("分页获取待审核的实名认证信息列表")
    @GetMapping("/auth-pending-list")
    public Result getAuthPendingList( RealnameAuthPageDTO realnameAuthPageDTO) {
        return ISysUserService.getAuthPendingList(realnameAuthPageDTO);
    }
    //	审核用户的实名认证申请
    @LogAsync("审核用户的实名认证申请")
    @PostMapping("/auth-verify")
    @Transactional
    public Result verifyRealnameAuth(@RequestBody @Valid AuthVerifyDTO authVerifyDTO) {
        return ISysUserService.verifyRealnameAuth(authVerifyDTO);
    }

}